package com.example.smartcity.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.User;
import com.example.smartcity.Entity.login;
import com.example.smartcity.Mappers.CollectionMapper;
import com.example.smartcity.Mappers.UserMapper;
import com.example.smartcity.Service.LoginAndRegisterService;
import com.example.smartcity.util.Utils;
import com.example.smartcity.utils.MybatisUtils;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("LoginAndRegisterService")
public class LoginAndRegisteerServiceImpl implements LoginAndRegisterService {
    public String login(login log, HttpServletResponse response) {
        // 调用UserMapper进行用户名和密码的验证
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.getUserByUserNameAndPassword(log.getUsername(), log.getPassword());
        sqlSession.close();
        //Userinfo userinfo = userDao.selectByEmail(email);
//        Map<String, String> map = new HashMap<>();
        JSONObject map = new JSONObject();
        if(user == null || user.toString()==""){
            map.put("result", "该用户不存在");
            return JSONObject.toJSONString(map);
        }
        if(user.getPassword().equals(log.getPassword())){
            String token = Utils.createJWT(user.getUid());
            response.setHeader("token", token);
            map.put("token", user.getUid());
            map.put("result", "success");
            //String headpic=resource_prefix + userinfo.getHeadpicture();
            //userinfo.setHeadpicture(headpic);
            //System.out.println(headpic);
            //map.put("user", userinfo);
        }else {
            map.put("result", "密码不正确");

        }
        return map.toJSONString();
    }
    public String register(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        try {
            userMapper.addUser(user.getNickname(),user.getEmail(),user.getUsername(),user.getPassword(),user.getSexual());
            sqlSession.commit();
            sqlSession.close();
        }catch (Exception e){
            System.out.println(e);
            System.out.println("注册失败");
        }
        JSONObject map = new JSONObject();
        if (userMapper.getUserByUserName(user.getUsername())!=null){
            map.put("code",500);
            map.put("username","用户名已存在");
            map.put("password","用户名已存在");
            return JSONObject.toJSONString(map);
        }else{
            map.put("code",200);
            map.put("username",user.getUsername());
            map.put("password",user.getPassword());
            return JSONObject.toJSONString(map);
        }
    }
    public String getUserAvatar(int uid) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        String avatarurl = userMapper.getUserAvatarById(uid);
        JSONObject map = new JSONObject();
        map.put("avatar",avatarurl);
        sqlSession.close();
        return avatarurl;
    }
    public String updateUserAvatar(String uid,String avatar) {
        return "hello";
    }
    public User getUserByUid(int uid){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserByUid(uid);
        sqlSession.close();
        return user;
    }
    public void updateUserInfo(Map<String, Object> map){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.updateUserByUid(map);
        sqlSession.commit();
        sqlSession.close();
    }
    public void updateUserPassword(Map<String, Object> map){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.updateUserPasswordByUid(map);
        sqlSession.commit();
        sqlSession.close();
    }
    public void updateUserAvatar(int uid,String avatar){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    }
    public void setNewCollection(int uid,int newsid){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);
        collectionMapper.addToCollection(uid,newsid);
        sqlSession.commit();
        sqlSession.close();
    }
    public void deleteCollection(int uid,int newsid){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    }

}
